package cn.hs.controller;

import cn.hs.entity.Mynotice;
import cn.hs.service.MynoticeService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName MynoticeController
 * @Description TODO
 * @Author CnYuu
 * @Date 2024年10月23日 19:05
 * @Version 1.0
 */
@RestController
@RequestMapping("/mynotice")
public class MynoticeController {
    @Autowired
    private MynoticeService mynoticeService;
    @GetMapping("/find")
    public Object find(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Mynotice mynotice
    ){
        PageInfo pageInfo = mynoticeService.findAll(pageNum, pageSize,mynotice);
        return  Result.success(pageInfo);
    }
    @GetMapping("/selectByUserId")
    public Object selectByUserId(
            @RequestParam Integer userId
    ){
        List<Mynotice> pageInfo = mynoticeService.selectByUserId(userId);
        return  Result.success(pageInfo);
    }
    @PostMapping("/insert")
    public Object insert(@RequestBody Mynotice mynotice){
        mynotice.setCreatetime(new Date());
        int insert = mynoticeService.insert(mynotice);
        if(insert!=0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @PostMapping("/update")
    public Object update(@RequestBody Mynotice mynotice){
        mynotice.setCreatetime(new Date());
        int update = mynoticeService.update(mynotice);
        if(update!=0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @PostMapping("/delete")
    public Object delete(@RequestBody Mynotice mynotice){
        int delete= mynoticeService.delete(mynotice.getId());
        if(delete!=0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
